//
//  HNWalk.m
//  HeroNuts
//
//  Created by Vicky on 5/4/11.
//  Copyright 2011 Tongji University. All rights reserved.
//

#import "HNWalk.h"
#import "HNActor.h"
#import "HNLogicPosition.h"
#import "HNActionCache.h"
#import "HNActorInfoCache.h"
#import "HNAudioEngine.h"

@interface HNWalk(assist) 

-(void) turnAroundWithActor:(NSString *)actor;
-(void) moveWithActor:(NSString *)actor by:(CGPoint)distance;
-(void) toggleFaceDirection;
-(void) playSoundEffect;
-(void) logicMove;

@end


@implementation HNWalk

#pragma mark HNWalk - initialization - preparing the action sequence to be run

-(id) initWithActorName:(NSString *)actor walkPath:(NSMutableArray *)path faceRight:(BOOL)faceRight
{
    if ((self = [super init])) {
        NSAssert(path!=nil , @"Attempted to pass a nil to 'path'!");
        
        actions_ = [[NSMutableArray alloc] init];
        
        CGFloat previousXDirection;
        previousXDirection = faceRight ? 1 : -1;
		
		//move_start
		CCAnimate *moveStart = [[HNActionCache sharedActionCache] actionByName:@"MOVE_START" actor:actor];
		if (moveStart) {
			[actions_ addObject:moveStart]; 
		}
        
        //prepare for logic movement
        HNLogicPosition *destination_logicPoisition = [path objectAtIndex:[path count]-1];
        destination_ = [destination_logicPoisition position];
        
		//moving
        for (int i=0;i<[path count]-1;i++) {
            HNLogicPosition *currentPosition = [path objectAtIndex:i];
            HNLogicPosition *nextPosition = [path objectAtIndex:i+1]; 
            CGPoint distance = [HNLogicPosition distanceInPixelsFrom:[currentPosition position] to:[nextPosition position]];
            
            //turn around when needed, add to actions_
            if (distance.x*previousXDirection < 0) {
                [self turnAroundWithActor:actor];
                previousXDirection = -1*previousXDirection;
            } 
            
            //move while running animation, add to actions_
            [self moveWithActor:actor by:distance]; 
        }
		
		//move_end
		CCAnimate *moveEnd = [[HNActionCache sharedActionCache] actionByName:@"MOVE_END" actor:actor];
		if (moveEnd) {
			[actions_ addObject:moveEnd]; 
		}
        
    }
    
    return self;
}

-(void) turnAroundWithActor:(NSString *)actor
{
    CCAnimate *turnLeft = [[HNActionCache sharedActionCache] actionByName:@"TURN_L" actor:actor];
    CCAnimate *turnRight = [[HNActionCache sharedActionCache] actionByName:@"TURN_R" actor:actor];
    CCCallFunc *toggleFace = [CCCallFunc actionWithTarget:self selector:@selector(toggleFaceDirection)];

    [actions_ addObject:turnLeft];
    [actions_ addObject:toggleFace];
    [actions_ addObject:turnRight];

}

-(void) toggleFaceDirection
{
    target_.faceRight = !target_.faceRight;
}

-(void) moveWithActor:(NSString *)actor by:(CGPoint)distance
{
    CCAnimate *walk = [[HNActionCache sharedActionCache] actionByName:@"MOVING" actor:actor];
    
    CCMoveBy *move = [CCMoveBy actionWithDuration:walk.duration position:distance];    
    CCCallFunc *playEffect = [CCCallFunc actionWithTarget:self selector:@selector(playSoundEffect)];
    CCCallFunc *logicMove = [CCCallFunc actionWithTarget:self selector:@selector(logicMove)];
    
    CCSpawn *moveWithAnimationAndSound = [CCSpawn actions:move,walk,playEffect,logicMove,nil];
    
    [actions_ addObject:moveWithAnimationAndSound];

}

- (void)playSoundEffect
{
    [[HNAudioEngine sharedEngine] playSoundEffectWithActor:target_.name action:@"MOVING"];
}

-(void) logicMove
{
    target_.position = destination_;
}

@end
